package com.example.dense_rack_server.controller.dataBackup;


import cn.hutool.json.JSONObject;
import com.example.dense_rack_server.service.dataBackup.BackupTaskService;
import com.example.dense_rack_server.service.dataBackup.NodeInfoService;
import com.example.dense_rack_server.utils.ResponseModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 备份任务信息 前端控制器
 * </p>
 *
 * @author  张彬 
 * @since 2025-07-01
 */
@Controller
@RequestMapping("/backup/task")
public class BackupTaskController {

    @Resource
    public BackupTaskService backupTaskService;
    @Resource
    public NodeInfoService nodeInfoService;

    /**
     * 定时任务首页
     */
    @GetMapping("/index")
    public ModelAndView list() {
        ModelAndView modelAndView = new ModelAndView("DataBackup/backupTask/index");
        modelAndView.addObject("nodeList", nodeInfoService.list());
        return modelAndView;
    }


    /**
     * 定时任务分页查询
     */
    @GetMapping("list")
    @ResponseBody
    public Map<String, Object> tableList(@RequestParam(value = "nodeId", required = false) String nodeId,
                                         @RequestParam(value = "status", required = false) String status,
                                         @RequestParam(value = "page") int page,
                                         @RequestParam(value = "limit") int limit) {
        return backupTaskService.tableList(page, limit, nodeId, status);
    }


    /**
     * 定时任务删除
     */
    @PostMapping("delete")
    @ResponseBody
    public ResponseModel delete(@RequestBody JSONObject data) {
        backupTaskService.removeById(data.getInt("id"));
        return ResponseModel.success("删除成功");
    }


    /**
     * 修改定时任务状态
     * @param data
     * @return
     */
    @PutMapping("/changeStatus")
    @ResponseBody
    public ResponseModel changeStatus(@RequestBody JSONObject data) {
        return backupTaskService.changeStatus(data);
    }
}
